/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// stdlib
#include <sys/time.h>   // hi-res clock (gettimeofday)

// autonet
#include "autopackets.h"

// properties
#include "camera2params.h"

// gcommon
#include "gexception.h"

// local
#include "imagestreamer.h"
#include "globaldata.h"



// --------------------- ImageStreamer::run() --------------------------------
void ImageStreamer::run()
{
	_image_id = 0;
	
	struct timeval clock;
	struct timeval end;
	gettimeofday( &clock, NULL );
	
	quint16 msecs = 0;
	
	// udp stream params
	QHostAddress targetAddress;
	quint16 targetPort = 0;

	// get jpeg interval
	quint16 jpegInterval = readInterval( GlobalData::instance()->getParams() );
	
	while( 1 )
	{
			// check for time interval
		if ( GlobalData::instance()->paramsNew() )
		{
			jpegInterval = readInterval( GlobalData::instance()->getParams() );
		}

			// check if address was updated
		if ( GlobalData::instance()->addressNew() )
		{
			targetAddress = GlobalData::instance()->getUdpAddress();
			targetPort = GlobalData::instance()->getUdpPort();
		}

		if ( ! targetAddress.isNull() && targetPort > 0 )
		{
				// check if timedout
			gettimeofday( &end, NULL );
			msecs = ( end.tv_sec - clock.tv_sec ) * 1000 + ( end.tv_usec - clock.tv_usec ) / 1000;

			if( ( msecs > jpegInterval ) && ( jpegInterval > 0 ) )
			{
				sendJpeg( targetAddress, targetPort );
					// clear timer 
				gettimeofday( &clock, NULL );
			}
		}

			// go to sleep, don't waste resources
		msleep( jpegInterval );
	}
}
// ---------------------------------------------------------------------------


// -------------------- ImageStreamer::readInterval() ------------------------
quint16 ImageStreamer::readInterval( const GParams & params )
{
	return params.value( PARAM_JPEG_INTERVAL ).toUInt();
}
// ---------------------------------------------------------------------------


// ---------------------- ImageStreamer::sendJpeg() --------------------------
void ImageStreamer::sendJpeg( const QHostAddress& address, quint16 port )
{
	// create packet
	GAutoFrame packet;
	packet.setImage( GlobalData::instance()->getJpeg( 1 ) );
	packet.setWidth( GlobalData::instance()->getImageWidth() ); // NOTE jpeg may have another size
	packet.setHeight( GlobalData::instance()->getImageHeight() );
	packet.setType( GAutoFrame::JPEG2K_YUV );
	packet.setId( _image_id ++ );
	
	// send packet
	QByteArray packetBuffer;
	QDataStream stream( &packetBuffer, QIODevice::WriteOnly );
	
	GHeader header = packet.header();
	
	stream << header << packet;
	
	_udpSender.sendSync( packetBuffer, address, port );
}
// ---------------------------------------------------------------------------





// eof
